class Clients::ProfilesController < ApplicationController
  before_filter :check_if_client

  def edit
    @client = current_user.client
    @user = current_user

    respond_to do |format|
      format.html
    end
  end

  def update
    @client = Client.find(current_user.client.id)
    @user = current_user
    valid_user = true

    respond_to do |format|
      if @client.valid?
        # If password has been changed
        unless params[:user][:password].blank?
          @user.password = params[:user][:password]
          @user.password_confirmation = params[:user][:password_confirmation]

          if @user.valid?
            @user.save
          else
            valid_user = false
            format.html { render :edit }
          end
        end

        if valid_user == true && @client.update_attributes(params[:client])
          flash[:notice] = "Su información ha sido actualizada."
          format.html { redirect_to root_path }
        else
          # Render view with errors.
          format.html { render :edit }
        end
      else
        # Render view with errors.
        format.html { render :edit }
      end
    end
  end

end
